<template>
  <div class="test">
    <div
      class="test-head"
      :class="{'head-line':!showList}"
    >
      <div class="test-white">
        <div class="rest-time">
          <img
            src="../assets/img/yellow-time.png"
            alt=""
          > {{restTime | MillisecondToDate}}
        </div>
        <div
          class="q-num"
          @click="showListBody"
        >
          <em>{{nowNum}}</em>/{{qNum}}
          <span :class="{'hide':showList}"></span>
        </div>
        <div class="commit">
          <span @click="commitQuestion">交 卷</span>
        </div>
      </div>
      <div
        class="q-list"
        v-show="showList"
      >
        <template v-for="(item,i) in qList">
          <span
            v-if="item.questionType==0"
            :class="{active:item.selfAnswer&&item.selfAnswer.length,'mark-span':i+1==nowNum}"
            @click="nowNum=i+1"
          >{{i+1}}
            <em
              class="red-point"
              v-if="item.beMark"
            ></em>
          </span>
          <span
            v-else
            :class="{active:item.selfAnswer[0]&&item.selfAnswer[0].answer,'mark-span':i+1==nowNum}"
            @click="nowNum=i+1"
          >{{i+1}}
            <em
              class="red-point"
              v-if="item.beMark"
            ></em>
          </span>
        </template>
        <!--<span class="mark-span">2</span>
                        <span>3</span>-->
      </div>
    </div>

    <transition
      v-for="(item,i) in qList"
      name="list"
      :key="i"
    >
      <div
        class="q-div"
        v-show="nowNum===(i+1)"
      >
        <div class="q-cart-head">
        </div>
        <div class="q-cart-line">
        </div>
        <div class="q-type">
          <div>{{item.questionType==0?'选择题':'填空题'}}</div>
          <div>{{item.score}}分</div>
        </div>
        <div
          class="q-body"
          v-html="item.body"
        >
        </div>
        <ul
          :id="'answer'+i"
          v-if="item.questionType==0&&item.answerConfig&&item.answerConfig.items"
          class="answer-list"
        >
          <li
            v-for="(aItem,aI) in item.answerConfig.items"
            @click="item.selfAnswer=aItem"
            :class="{active:item.selfAnswer===aItem}"
            :key="aI"
          >
            {{aItem}}
          </li>
        </ul>
        <ul
          :id="'answer'+i"
          v-if="item.questionType==1&&item.answerConfig"
          class="input-list"
        >
          <li
            class="active"
            v-for="(iI,index) in item.answerConfig.inputCount"
            :key="index"
          >
            <div>{{iI}}、</div>
            <div class="input-answer">
              <textarea
                name=""
                v-model="item.selfAnswer[iI-1].answer"
                id=""
                rows="2"
                placeholder="请在此作答"
              ></textarea>
            </div>
          </li>
        </ul>
        <div class="mark">
          <span
            @click="item.beMark=!item.beMark"
            :class="{active:item.beMark}"
          >标记</span>
        </div>
      </div>
    </transition>
    <div class="border-div"></div>
    <div class="test-btn">
      <div
        v-show="nowNum!==1"
        class="q-last"
        @click="last"
      >上一题</div>
      <div
        class="q-next"
        @click="next"
      >{{nowNum!==qList.length?'下一题':'交卷'}}</div>
    </div>
  </div>
</template>

<script>
import api from 'util/xhr'
export default {
  name: 'test',
  components: {},
  data() {
    let question =
      sessionStorage.getItem('question') &&
      JSON.parse(sessionStorage.getItem('question'))
    console.log(question)
    let restTime = 0
    if (Number(sessionStorage.getItem('restTime')) > 0) {
      restTime = Number(sessionStorage.getItem('restTime'))
    } else if (
      question &&
      question.examPaperMirror &&
      question.examPaperMirror.examPaper &&
      question.status == '0' &&
      question.testDuration != '0'
    ) {
      restTime = Number(question.testDuration)
    } else {
      restTime = question.examPaperMirror.examPaper.testTime * 60 * 1000 || 0
    }
    console.log(Number(sessionStorage.getItem('restTime')))
    return {
      showList: false,
      question: question,
      nowNum: Number(sessionStorage.getItem('nowNum')) || 1,
      qNum:
        (question &&
          question.examPaperMirror &&
          question.examPaperMirror.examPaper &&
          question.examPaperMirror.examPaper.questionAmount) ||
        0,
      qList:
        (question &&
          question.examPaperMirror &&
          question.examPaperMirror.questions) ||
        '',
      restTime: restTime,
      downInter: '',
      saveInter: '',
      answerDetail: []
    }
  },
  computed: {},
  mounted() {},
  watch: {
    qList: {
      handler: function(val) {
        console.log(val)
        let answers = []
        val.map(item => {
          if (item.questionType == 0 && item.selfAnswer) {
            answers.push({
              examQuestionId: item.examQuestionId,
              answer: [item.selfAnswer]
            })
          } else if (
            item.questionType == 1 &&
            item.selfAnswer &&
            item.selfAnswer.length
          ) {
            let inputAnswer = []
            item.selfAnswer.map(sItem => {
              inputAnswer.push(sItem.answer.trim())
            })
            answers.push({
              examQuestionId: item.examQuestionId,
              answer: JSON.parse(JSON.stringify(inputAnswer))
            })
          }
        })
        this.question.examPaperMirror.questions = val
        this.answerDetail = answers
        sessionStorage.setItem('question', JSON.stringify(this.question))
      },
      deep: true
    }
  },
  created() {
    this.countDown()
    this.autoSave()
    if (!this.question) {
      this.$router.replace('/assessment')
    }
  },
  destroyed() {
    sessionStorage.removeItem('nowNum')
    sessionStorage.removeItem('question')
    sessionStorage.removeItem('restTime')
    this.saveInter && clearInterval(this.saveInter)
    this.downInter && clearInterval(this.downInter)
  },
  methods: {
    saveTestResult(isComplete) {
      api
        .post('/api/m/saveTestResult', {
          onlineTestResultId: this.question.onlineTestResultId,
          testDuration: this.restTime + '',
          answerDetail: JSON.stringify(this.answerDetail),
          isComplete: isComplete || false
        })
        .then(
          data => {
            if (isComplete) {
              sessionStorage.removeItem('nowNum')
              sessionStorage.removeItem('question')
              sessionStorage.removeItem('restTime')
              this.saveInter && clearInterval(this.saveInter)
              this.downInter && clearInterval(this.downInter)
              this.$router.replace({
                path: '/test-result',
                query: {
                  onlineTestResultId: this.question.onlineTestResultId
                }
              })
            }
          },
          desc => {
            this.$alert(desc)
          }
        )
    },
    countDown() {
      this.downInter = setInterval(() => {
        this.restTime = this.restTime - 1000
        sessionStorage.setItem('restTime', this.restTime)
        if (this.restTime <= 0) {
          this.saveTestResult(true)
          clearInterval(this.downInter)
        }
      }, 1000)
    },
    autoSave() {
      this.saveInter = setInterval(() => {
        this.saveTestResult()
      }, 30 * 1000)
    },
    showListBody() {
      this.showList = !this.showList
    },
    last() {
      if (this.nowNum > 1) {
        this.nowNum--
      }
      sessionStorage.setItem('nowNum', this.nowNum)
    },
    next() {
      console.log(this.question)
      if (this.nowNum < this.qNum) {
        this.nowNum++
      } else {
        this.commitQuestion()
      }
      sessionStorage.setItem('nowNum', this.nowNum)
    },
    commitQuestion() {
      let timeTip = ''
      let answerTip = '确定交卷？'
      if (this.restTime > 0) {
        timeTip = '考试时间未到，确定要交卷吗？'
      }
      console.log(this.answerDetail)
      if (!this.answerDetail.length) {
        answerTip = '您还有题目未答，确定要交卷吗？'
      }
      for (let i = 0; i < this.answerDetail.length; i++) {
        console.log(this.answerDetail[i])
        if (!this.answerDetail[i].answer[0].length) {
          answerTip = '您还有题目未答，确定要交卷吗？'
          break
        }
      }
      if (timeTip) {
        this.$confirm({ content: timeTip }).then(() => {
          this.saveTestResult(true)
        })
      } else {
        this.$confirm({ content: timeTip }).then(() => {
          this.saveTestResult(true)
        })
      }
    }
  }
}
</script>
<style lang="less">
.q-body{
  img{
    max-width: 100% !important;
  }
}
</style>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.test {
  background: url('../assets/img/bg-start.png');
  background-size: cover;
  min-height: 100%;
  padding-top: 25px;
  padding-bottom: 70px;
  position: relative;
}

.test-head {
  background: #e5f2f7;
  margin: 0 20px 20px;
  border-top-left-radius: 40px;
  border-top-right-radius: 40px;
  border-bottom-left-radius: 20px;
  border-bottom-right-radius: 20px;
}

.head-line {
  border-bottom-left-radius: 40px;
  border-bottom-right-radius: 40px;
}

.test-white {
  background: white;
  display: flex;
  height: 80px;
  border-radius: 40px;
  align-items: center;
  font-weight: bold;
}

.rest-time {
  line-height: 60px;
  width: 256px;
  height: 60px;
  border-radius: 30px;
  color: #0050b1;
  font-size: 32px;
  background: #bee6ff;
  margin: 0 10px;
  font-weight: bold;
}

.rest-time img {
  position: relative;
  top: 6px;
  width: 34px;
  margin-left: 20px;
  /*margin-right: 1rem;*/
}

.q-list {
  padding: 40px 30px 10px;
  text-align: center;
}

.red-point {
  position: absolute;
  top: -10px;
  right: -10px;
  background: #ff7800;
  width: 20px;
  height: 20px;
  border-radius: 10px;
  display: inline-block;
}

.q-list span {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 60px;
  line-height: 60px;
  text-align: center;
  border-radius: 6px;
  font-size: 28px;
  font-weight: bold;
  color: white;
  margin: 0 15px;
  margin-bottom: 30px;
  border: 1px solid #0db1f5;
  background: #eee;
  color: #0db1f5;
}
.q-list span:hover {
  border: 1px solid #ff7800;
  color: #ff7800;
}

.q-list span.active {
  background: white;
}

.q-list span.mark-span {
  border: 1px solid #ff7800;
  color: #ff7800;
}

.q-num {
  flex: 1;
  text-align: center;
  font-size: 32px;
  color: #0050b1;
}

.q-num .hide {
  transform: rotate(180deg);
}

.q-num em {
  color: #ff7800;
}

.q-num span {
  position: relative;
  width: 0;
  height: 0;
  top: 25px;
  right: 0;
  content: '';
  border-top: 12px solid #0050b1;
  border-right: 6px solid transparent;
  border-left: 6px solid transparent;
}

.commit {
  width: 213px;
  display: flex;
  justify-content: flex-end;
}

.commit span {
  font-size: 28px;
  margin-right: 10px;
  display: inline-block;
  width: 120px;
  height: 60px;
  background-image: linear-gradient(0deg, #ffa113 0%, #ffc40c 100%);
  border-radius: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.q-div {
  margin: 0 20px;
  background: white;
  font-size: 28px;
}

.q-cart-head::after {
  content: '';
  width: 10px;
  height: 20px;
  border-radius: 0 10px 10px 0;
  background: #0050b2;
  position: absolute;
  top: 0;
  right: -10px;
}

.q-cart-head::before {
  content: '';
  width: 10px;
  height: 20px;
  border-radius: 10px 0 0 10px;
  background: #0050b2;
  position: absolute;
  top: 0;
  left: -10px;
}

.q-cart-head {
  position: relative;
  height: 10px;
  width: 100%;
  background: #0050b2;
}

.q-cart-line {
  height: 50px;
  background-image: linear-gradient(180deg, #fff 0%, #ddd 50%, #fff 100%);
}

.q-type {
  height: 60px;
  background: #f0f0f0;
  padding: 0 30px;
  color: #ff9600;
  display: flex;
  font-weight: bold;
  align-items: center;
  margin: 30px 25px 0 25px;
}

.q-type div {
  flex: 1;
}

.q-type div:last-child {
  display: flex;
  justify-content: flex-end;
}

.q-body {
  padding: 50px;
  line-height: 45px;
}

.answer-list {
  padding: 0 50px;
}

.answer-list li {
  height: 60px;
  border: 1px dashed #7d7d7d;
  margin-bottom: 30px;
  border-radius: 12px;
  display: inline-block;
  text-align: center;
  width: 60px;
  line-height: 60px;
  margin-right: 40px;
}

.answer-list li.active {
  border: 1px dashed #ff9600;
  background: #fff4e4;
}

.mark {
  padding: 100px 20px 30px 0;
  display: flex;
  justify-content: flex-end;
}

.mark span {
  display: inline-block;
  width: 68px;
  height: 68px;
  border-radius: 68px;
  background: #bbd1ca;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  color: white;
  font-weight: bold;
}

.mark span.active {
  background: #ff9600;
}

.test-btn {
  margin-top: 40px;
  display: flex;
  padding-bottom: 20px;
  position: fixed;
  bottom: 20px;
  width: 100%;
  height: 70px;
}

.test-btn div {
  width: 190px;
  height: 70px;
  border: 4px solid white;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 37px;
  font-size: 32px;
  color: white;
  font-weight: bold;
}

.q-last {
  margin-left: 80px;
  background-image: linear-gradient(0deg, #0e9bf5 0%, #50c4e2 100%);
}

.q-next {
  position: absolute;
  right: 80px;
  background-image: linear-gradient(0deg, #ff9f13 0%, #ffc60b 100%);
}

.border-div {
  margin: 0 20px;
  height: 10px;
  background: url('../assets/img/banyuan.png') 50%;
}

.input-list {
  padding: 0 50px;
}

.input-list li {
  border: 1px dashed #7d7d7d;
  border-radius: 12px;
  display: flex;
  padding: 20px;
  margin-bottom: 10px;
}

.input-list li textarea {
  font-size: 28px;
  border: 0;
  width: 100%;
}

.input-answer {
  flex: 1;
}

.list-enter-active,
.list-leave-active {
  transition: all 0.5s ease-out;
}

/* 进入开始 */

.list-enter {
  transform: translateY(100%);
  opacity: 0;
}

/* 出去终点 */

.list-leave-active {
  transform: translateY(-100%);
  opacity: 0;
}
</style>
<style>
.q-div img {
  vertical-align: middle !important;
  min-height: 30px;
}
img.latex {
  /*height: calc( 100% + 2rem) !important;
    vertical-align: middle !important;*/
}
</style>

